﻿using MES.Desktop.Common;
using MES.Desktop.IService;
using MES.Desktop.SystemModule.Models;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MES.Desktop.SystemModule.ViewModels.Dialogs
{
    public class ModifyPostViewModel : DialogViewModelBase
    {
        public PostModel PostInfo { get; set; } =
                new PostModel();


        IPostService _postService;
        public ModifyPostViewModel(IPostService postService)
        {
            _postService = postService;
            PostInfo = new PostModel(postService);
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            var model = parameters.GetValue<PostModel>("model");

            if (model == null)
            {
                Title = "新增职位信息";
            }
            else
            {
                Title = "编辑职位信息";

                PostInfo.PostId = model.PostId;
                PostInfo.PostName = model.PostName;
            }
        }

        public override void DoSave()
        {
            if (PostInfo.HasErrors) return;
            try
            {
                Entities.PostEntity postEntity = new Entities.PostEntity
                {
                    PostId = PostInfo.PostId,
                    PostName = PostInfo.PostName
                };

                _postService.UpdatePost(postEntity);

                base.DoSave();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    }
}
